import { Effect, EffectType } from '../types/effect';

export class EffectManager {
  private effects: Effect[] = [];

  addEffect(effect: Effect): void {
    const timestamp = Date.now();
    
    if (effect.stackable) {
      this.effects.push({ ...effect, timestamp });
    } else {
      const existingIndex = this.effects.findIndex(e => e.type === effect.type);
      if (existingIndex >= 0) {
        this.effects[existingIndex] = { ...effect, timestamp };
      } else {
        this.effects.push({ ...effect, timestamp });
      }
    }
  }

  updateEffects(): void {
    this.effects = this.effects
      .map(effect => ({ ...effect, duration: effect.duration - 1 }))
      .filter(effect => effect.duration > 0);
  }

  getEffectValue(type: EffectType): number {
    return this.effects
      .filter(effect => effect.type === type)
      .reduce((total, effect) => total + effect.value, 0);
  }

  hasEffect(type: EffectType): boolean {
    return this.effects.some(effect => effect.type === type);
  }

  clearEffects(): void {
    this.effects = [];
  }

  getActiveEffects(): Effect[] {
    return [...this.effects];
  }
}
